﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AlphaBeatSoundEngine
{
    public partial class TestForm : Form
    {
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new TestForm());
        }

        private AlphaBeat _alphaBeat;
        public TestForm()
        {
            InitializeComponent();
        }

        private void AlphaBeatWillPlayLetter(object sender, AlphaBeatWillPlayLetterEventArgs e)
        {
            char currentChar = _alphaBeat.CurrentWord.ToCharArray()[e.LetterIndex];
            Console.WriteLine("WinForm : AB will play letter {0} '{1}'", e.LetterIndex, currentChar);
            this.Invoke((MethodInvoker)delegate
            {
                currentLetter.Text = currentChar.ToString(); // runs on UI thread
            });
        }

        private void AlphaBeatDidStop(object sender, AlphaBeatDidStopEventArgs e)
        {
            this.Invoke((MethodInvoker)delegate
            {
                currentLetter.Text = ""; // runs on UI thread
            });
        }

        private void TestForm_Load(object sender, EventArgs e)
        {
            //init alphabeat engine
            bool _success = true;
            try
            {
                _alphaBeat = new AlphaBeat();
                _alphaBeat.AlphaBeatWillPlayLetter += this.AlphaBeatWillPlayLetter;
                _alphaBeat.AlphaBeatDidStop += this.AlphaBeatDidStop;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Erreur : "+ ex.Message);
                _success = false;
            }
            if (!_success)
            {
                this.Close();
            }

            //filling style combobox
            List<MusicStyle> styles = MusicStyle.MusicStyles;
            styleComboBox.DataSource = styles;
            fundamentaleComboBox.DataSource = Enum.GetValues(typeof(Tone)); ;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                try
                {
                    _alphaBeat.DefineWord(textBox1.Text);
                }
                catch (NoPatternForGivenWordLengthException ex) 
                {
                    currentLetter.Text = "?";
                }
                
                if (!_alphaBeat.IsStarted)
                {
                    _alphaBeat.Start();
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            _alphaBeat.Stop();
        }

        private void TestForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if(_alphaBeat!=null)
                _alphaBeat.Stop();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void styleComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<MusicPattern> patterns = MusicStyle.MusicStyles.ElementAt(styleComboBox.SelectedIndex).GetAvailablePatternsForWordLength((int)wordLengthNumericUpDown.Value);
            patternComboBox.DataSource = patterns;
        }

        private void wordLengthNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            List<MusicPattern> patterns = MusicStyle.MusicStyles.ElementAt(styleComboBox.SelectedIndex).GetAvailablePatternsForWordLength((int)wordLengthNumericUpDown.Value);
            patternComboBox.DataSource = patterns;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            MusicStyle style = MusicStyle.MusicStyles[styleComboBox.SelectedIndex];
            MusicPattern pattern = style.GetPatternByIndexForWordLength((int)wordLengthNumericUpDown.Value, patternComboBox.SelectedIndex);
            Tone fundamentale = (Tone) fundamentaleComboBox.SelectedItem;
            MusicalWord word = new MusicalWord(style, pattern, fundamentale);
            _alphaBeat.ForceMusicalWord(word);
            _alphaBeat.Start();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            _alphaBeat.Stop();
        }
    }
}
